# -*- coding: utf-8 -*-

from odoo import models, fields, api


class CpqQuotationLine(models.Model):
    _name = 'cpq.quotation.line'
    _description = 'CPQ报价明细行'
    _order = 'sequence, id'
    
    # 基本信息
    quotation_id = fields.Many2one(
        'cpq.quotation',
        string='报价单',
        required=True,
        ondelete='cascade'
    )
    
    sequence = fields.Integer(
        string='序号',
        default=10
    )
    
    # 产品信息
    product_id = fields.Many2one(
        'product.product',
        string='产品',
        required=True
    )
    
    name = fields.Char(
        string='描述',
        required=True
    )
    
    # 数量和价格
    quantity = fields.Float(
        string='数量',
        default=1.0,
        required=True
    )
    
    unit_cost = fields.Monetary(
        string='单位成本',
        currency_field='currency_id'
    )
    
    unit_price = fields.Monetary(
        string='单价',
        currency_field='currency_id',
        required=True
    )
    
    subtotal = fields.Monetary(
        string='小计',
        compute='_compute_subtotal',
        store=True,
        currency_field='currency_id'
    )
    
    currency_id = fields.Many2one(
        'res.currency',
        related='quotation_id.currency_id',
        string='货币'
    )
    
    @api.depends('quantity', 'unit_price')
    def _compute_subtotal(self):
        for line in self:
            line.subtotal = line.quantity * line.unit_price

